﻿#region

using System;
using System.Web;

#endregion

namespace CuongHanh.Extensions
{
    public static class SessionExtension
    {
        public static string GetCustomerName(this HttpSessionStateBase session)
        {
            return session[AppUtil.CUSTOMERNAME] != null
                    ? session[AppUtil.CUSTOMERNAME] + ""
                    : string.Empty;
        }

        public static int GetCustomerId(this HttpSessionStateBase session)
        {
            int customerId = 0;
            if(session[AppUtil.CUSTOMERID] != null)
                int.TryParse(session[AppUtil.CUSTOMERID] + "",
                        out customerId);
            return customerId;
        }

        public static void WriteCustomerInfo(this HttpSessionStateBase session,
                int customerId,
                string customerName)
        {
            session[AppUtil.CUSTOMERID] = customerId;
            string[] array = customerName.Split(new char[] {' '},
                    StringSplitOptions.RemoveEmptyEntries);

            session[AppUtil.CUSTOMERNAME] = array.Length > 0
                    ? array[array.Length - 1].ToUpper()
                    : string.Empty;
        }
    }
}
